//https://leetcode.cn/problems/1fGaJU/
class Solution {
public:
    vector<vector<int>> threeSum(vector<int>& nums) {
        vector<vector<int>> vv;
        sort(nums.begin(), nums.end());

        for (int i = 0; i < nums.size(); i++)
        {
            if (nums[i] > 0)
            {
                break;
            }

            if (i > 0 && nums[i - 1] != nums[i] || i == 0)
            {
                int left = i + 1, right = nums.size() - 1;
                while (left < right)
                {
                    while (left < right && left > i + 1 && nums[left] == nums[left - 1])
                    {
                        left++;
                    }
                    while (left < right && right < nums.size() - 1 && nums[right] == nums[right + 1])
                    {
                        right--;
                    }

                    if (left < right)
                    {
                        if (nums[left] + nums[right] < -nums[i])
                        {
                            left++;
                        }
                        else if (nums[left] + nums[right] > -nums[i])
                        {
                            right--;
                        }
                        else
                        {
                            vector<int> tmp;
                            tmp.push_back(nums[i]);
                            tmp.push_back(nums[left]);
                            tmp.push_back(nums[right]);
                            vv.push_back(tmp);
                            left++;
                            right--;
                        }
                    }
                }
            }
        }

        return vv;
    }
};